//
//  YuePayViewController.m
//  YaDiuDiu
//
//  Created by 欧洪飞 on 2017/10/20.
//  Copyright © 2017年 jy. All rights reserved.
//

#import "YuePayViewController.h"

#import "SYPasswordView.h"
#import "PaySuccessViewController.h"
@interface YuePayViewController ()<SYPasswordViewDelegate>



@end

@implementation YuePayViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = COLOR_RGB(242, 242, 242, 1);
    
    [self loadNav];
    [self loadUI];
}

- (void)loadNav{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 64)];
    view.backgroundColor = [UIColor whiteColor];
    view.userInteractionEnabled = YES;
    [self.view addSubview:view];
    
    UILabel *lab = [[UILabel alloc] init];
    lab.text = @"余额支付";
    lab.textColor = [UIColor blackColor];
    lab.font = [UIFont systemFontOfSize:18.0];
    [view addSubview:lab];
    [lab makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(view.centerX);
        make.top.equalTo(34);
    }];
    
    UILabel *line = [[UILabel alloc] init];
    line.backgroundColor = [UIColor lightGrayColor];
    [view addSubview:line];
    [line makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(0);
        make.right.equalTo(0);
        make.bottom.equalTo(view.bottom);
        make.height.equalTo(0.5);
    }];
    
    //返回按钮
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    //btn.backgroundColor = [UIColor redColor];
    [btn setImage:[UIImage imageNamed:@"箭头"] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:@"箭头"] forState:UIControlStateHighlighted];
    [btn addTarget:self action:@selector(clickBack) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:btn];
    [btn makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(lab.centerY);
        make.left.equalTo(0);
        make.width.equalTo(40);
        make.height.equalTo(40);
    }];
}
// 返回
- (void)clickBack{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)loadUI{
    SYPasswordView *pasView = [[SYPasswordView alloc] initWithFrame:CGRectMake(16, 100, ScreenWidth-32,45)];
    [pasView.textField becomeFirstResponder];
    pasView.delegate = self;
    [self.view addSubview:pasView];
    
    
}
#pragma -mark SYPasswordViewDelegate
-(void)sendText:(NSString *)pass{
    NSLog(@"pass:%@",pass);
    if ([_state isEqualToString:@"提现"]) {
        [self loadTiXian:pass];
    }else{
        [self loadYuePay:pass];
    }
    
    
}
//提现
- (void)loadTiXian:(NSString *)pwd{
    [_dic setValue:pwd forKey:@"password"];
    NSLog(@"dic:%@",_dic);
    NSString *url = [NSString stringWithFormat:@"%@/App/Usercenter/withdraw",urlHead];
    [[AFHTTPSessionManager manager] POST:url parameters:_dic success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"提现res:%@",responseObject);
        NSString *error = [NSString stringWithFormat:@"%li",[[responseObject objectForKey:@"error"] integerValue]];
        if ([error integerValue] == 200) {
            
        }
        [LICGToast showWithText:[responseObject objectForKey:@"mssage"]];
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
    }];
}

//余额支付接口
- (void)loadYuePay:(NSString *)pwd{
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    [parmas setObject:[user objectForKey:@"userid"] forKey:@"userid"];
    [parmas setObject:_price forKey:@"money"];
    [parmas setObject:pwd forKey:@"password"];
    NSString *url = [NSString stringWithFormat:@"%@/App/Shoppingcart/balancepay",urlHead];
    NSLog(@"parmas:%@",parmas);
    [[AFHTTPSessionManager manager] POST:url parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"余额支付接口res:%@",responseObject);
        NSString *error = [NSString stringWithFormat:@"%li",[[responseObject objectForKey:@"error"] integerValue]];
        if ([error integerValue] == 200) {
            
            [self dismissViewControllerAnimated:YES completion:nil];
            [self.delegate sendState:@"成功"];
        }else{
            [self showAlert:@"支付密码错误"];
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
